import Vue from 'vue'
import Router from 'vue-router'
import login from './components/login'
import home from './components/home'
import Welcome from './components/welcome'
import users from './components/user/users'
import rights from './components/power/rights'
import roles from './components/power/roles'
import cates from './components/goods/cates'
import params from './components/goods/params'
import goods from './components/goods/goods'
import goodAdd from './components/goods/goodAdd'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: login
    },
    {
      path: '/home',
      component: home,
      redirect: '/welcome',
      children: [
        {
          path: '/welcome',
          component: Welcome
        },
        {
          path: '/users',
          component: users
        },
        {
          path: '/rights',
          component: rights
        },
        {
          path: '/roles',
          component: roles
        },
        {
          path: '/categories',
          component: cates
        },
        {
          path: '/params',
          component: params
        },
        {
          path: '/goods',
          component: goods
        },
        {
          path: '/goods/add',
          component: goodAdd
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = sessionStorage.getItem('token')
  if (!token) return next('/login')

  next()
})
export default router
